package ch.penas.web;

import java.io.Serializable;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;

/** @author Daniel */
@ManagedBean(name = "sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
	
	/** Serialisation de la class SessionBean pour une question de securiter. */
	private static final long serialVersionUID = 1193823934382880758L;
	
	/** mLanguages permet d'acc�der � la liste Locale. */
	private static List<Locale> mLanguages;
	
	/**  mLocale permet d'acc�der � la class Locale. */
	private Locale mLocale;
	
	static {
		Locale itIT = new Locale(Locale.ITALIAN.getLanguage(), "IT");
		Locale enUS = new Locale(Locale.ENGLISH.getLanguage(), "US");
		Locale frCH = new Locale(Locale.FRENCH.getLanguage(), "CH");
		Locale geDE = new Locale(Locale.GERMAN.getLanguage(), "DE");
		
		mLanguages = new ArrayList<Locale>();
		mLanguages.add(frCH);
		mLanguages.add(enUS);
		mLanguages.add(itIT);
		mLanguages.add(geDE);
		
	}

	/**
	 * Permet de recup�r� le nom de l'utilisateur (admin, user).
	 * 
	 * @return guest
	 */
	public final String getUsername() {
		Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
		if (principal != null) {
			return principal.getName();
		}
		return "guest";
	}

	/** Permet de retourner la langue.
	 * 
	 * @return mLanguages
	 */
	public final Collection<Locale> getCountries() {
		return mLanguages;
	}

	/** Permet de retourner le code de la localiter.
	 * 
	 * @return mLocale
	 */
	public final Locale getLocaleCode() {
		if (this.mLocale == null) {
			return FacesContext.getCurrentInstance().getViewRoot().getLocale();
		}
		return this.mLocale;
	}

	/** Permet de modifier le code de la localiter.
	 * 
	 * @param localeCode est egale � mLocale
	 */
	public final void setLocaleCode(final Locale localeCode) {
		this.mLocale = localeCode;
	}

	/** Permet de changer la langue.
	 * 
	 * @param e est egale � ValueChangeEvent
	 */
	public final void countryLocaleCodeChanged(final ValueChangeEvent e) {
		Locale newLocale = (Locale) e.getNewValue();

		if (newLocale == null) {
			FacesMessage msg = new FacesMessage("Unknown locale");
			msg.setSeverity(FacesMessage.SEVERITY_FATAL);
			throw new ValidatorException(msg);
		}
		FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
	}

	/** Permet de quitter la session en coure.
	 * 
	 * @return logout
	 */
	public final String logout() {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
		if (session != null) {
			session.invalidate();
		}
		return "logout";
	}

}
